<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Case Reporting Controller.
 * 
 * @class     Case Reporting
 * @author    Iqbal
 * @version   1.0.0
 * @copyright Copyright (c) 2014 IPPI (http://www.ippi.or.id)
 */
class case_reporting extends CI_Controller {
    /**
     * Member Logged
     * 
     * @var object $member_logged
     */
    public $member_logged;
    
    /**
	 * Constructor.
	 */
    function __construct() {
        parent::__construct();
        
        // Load model
        $this->load->model('model_kasus', 'm_kasus', TRUE);
        
        // Check is member logged in
        is_member_logged_in();
        
        // Set member logged
        $this->member_logged        = ippi_get_current_member();
    }
    
    /**
	 * Insert case reporting data function. 
     * @return AJAX String
	 */
    function insert(){        
        // Set current date for datecreated and datemodified
        $curdate        = date("Y-m-d h:i:s");
        $member_logged  = $this->member_logged;
        $saksi          = ( ippi_isset($this->input->post("saksi_value"), '') ? $this->input->post("saksi_value") : false );
        $us             = ( ippi_isset($this->input->post("us"), '') ? $this->input->post("us") : false );
        
        // Set case reporting data
        $case_data      = array(
            'alias'             => ( $saksi ? 'saksi' : 'korban' ),
            'korban_name'       => ippi_isset($this->input->post("korban_name"), ''),
            'korban_tgllahir'   => date( "Y-m-d", strtotime(ippi_isset($this->input->post("korban_tgllahir"), '')) ),
            'korban_idcard'     => ippi_isset($this->input->post("korban_idcard"), ''),
            'korban_statushiv'  => ippi_isset($this->input->post("korban_statushiv"), ''),
            'korban_propinsi'   => ippi_isset($this->input->post("korban_propinsi"), ''),
            'korban_kota'       => ippi_isset($this->input->post("korban_kota"), ''),
            'korban_alamat'     => ippi_isset($this->input->post("korban_alamat"), ''),
            'korban_hp'         => ippi_isset($this->input->post("korban_hp"), ''),
            'korban_email'      => ippi_isset($this->input->post("korban_email"), ''),
            
            'dk_lokasi'         => ippi_isset($this->input->post("dk_lokasi"), ''),
            'dk_propinsi'       => ippi_isset($this->input->post("dk_propinsi"), ''),
            'dk_kota'           => ippi_isset($this->input->post("dk_kota"), ''),
            'dk_alamat'         => ippi_isset($this->input->post("dk_alamat"), ''),
            'dk_deskripsi'      => ippi_isset($this->input->post("dk_deskripsi"), ''),
            
            'datecreated'       => $curdate,
            'datemodified'      => $curdate,
            'createdby'         => $member_logged->id_member,
            'modifiedby'        => $member_logged->id_member,
        );
        
        // Set if undefined suspect
        if( $us ){
            $case_data['pelaku_jk']         = ippi_isset($this->input->post("pelaku_jkus"), '');
            $case_data['pelaku_warkul']     = ippi_isset($this->input->post("pelaku_warkul"), '');
            $case_data['pelaku_tinggi']     = ippi_isset($this->input->post("pelaku_tinggi"), '');
            $case_data['pelaku_tanda']      = ippi_isset($this->input->post("pelaku_tanda"), '');
            $case_data['pelaku_karakter']   = ippi_isset($this->input->post("pelaku_karakter"), '');
            $case_data['pelaku_ciwaj']      = ippi_isset($this->input->post("pelaku_ciwaj"), '');
        }else{
            $case_data['pelaku_name']       = ippi_isset($this->input->post("pelaku_name"), '');
            $case_data['pelaku_nick']       = ippi_isset($this->input->post("pelaku_nick"), '');
            $case_data['pelaku_jk']         = ippi_isset($this->input->post("pelaku_jk"), '');
            $case_data['pelaku_noid']       = ippi_isset($this->input->post("pelaku_noid"), '');
            $case_data['pelaku_hub']        = ippi_isset($this->input->post("pelaku_hub"), '');
            $case_data['pelaku_telp']       = ippi_isset($this->input->post("pelaku_telp"), '');
            $case_data['pelaku_alamat']     = ippi_isset($this->input->post("pelaku_alamat"), '');
            $case_data['pelaku_email']      = ippi_isset($this->input->post("pelaku_email"), '');
        }
        
        // Save case reporting
        $save = $this->m_kasus->insert_case($case_data);
        
        // Check response of save case reporting data
        if($save && !empty($save)){
            $jenis_kasus        = ippi_isset($this->input->post("dk_jenis"), '');
            
            if(!empty($jenis_kasus)){
                foreach($jenis_kasus as $jk){
                    $data_jk    = array(
                        'id_kasus'      => $save,
                        'jenis_kasus'   => $jk,
                        'keterangan'    => '',
                        'datecreated'   => $curdate,
                        'datemodified'  => $curdate,
                        'createdby'     => $member_logged->id_member,
                        'modifiedby'    => $member_logged->id_member,
                    );
                    $this->m_kasus->insert_case_type($data_jk);
                }
            }
            
            if($saksi){
                $data_saksi     = array(
                    'id_kasus'      => $save,
                    'nama'          => ippi_isset($this->input->post("saksi_name"), ''),
                    'tgllahir'      => date( "Y-m-d", strtotime(ippi_isset($this->input->post("saksi_tgllahir"), '')) ),
                    'noid'          => ippi_isset($this->input->post("saksi_idcard"), ''),
                    'propinsi'      => ippi_isset($this->input->post("saksi_propinsi"), ''),
                    'kota'          => ippi_isset($this->input->post("saksi_kota"), ''),
                    'alamat'        => ippi_isset($this->input->post("saksi_alamat"), ''),
                    'telp'          => ippi_isset($this->input->post("saksi_telp"), ''),
                    'email'         => ippi_isset($this->input->post("saksi_email"), ''),
                    'hubungan'      => ippi_isset($this->input->post("saksi_hub"), ''),
                    'datecreated'   => $curdate,
                    'datemodified'  => $curdate,
                    'createdby'     => $member_logged->id_member,
                    'modifiedby'    => $member_logged->id_member     
                );
                $this->m_kasus->insert_case_saksi($data_saksi);
            }
            $output = array('case_id' => $save);
            echo json_encode($output);
        }else{
            echo '0';
        }
    }
    
    /**
	 * Follow up case reporting data function. 
     * @return AJAX String
	 */
    function case_followup(){
        // Set variable
        $curdate        = date("Y-m-d h:i:s");
        $case_id        = ippi_isset($this->input->post("case_id"), '');
        
        // Set follow up case reporting data
        $case_tl_data   = array(
            'tl_value'      => ippi_isset($this->input->post("tl_value"), ''),
            'tl_telp'       => ippi_isset($this->input->post("tl_telp"), ''),
            'datemodified'  => $curdate
        );        
        
        // Update follow up case reporting data
        $update = $this->m_kasus->update_case_tl($case_tl_data, $case_id);
        
        if( $update )   { echo '1'; }
        else            { echo '0'; }
    }
    
    /**
	 * Preveiw case reporting data function. 
     * @return AJAX String
	 */
    function preview(){
        // Set current date for datecreated and datemodified
        $curdate        = date("Y-m-d h:i:s");
        $member_logged  = $this->member_logged;
        $saksi          = ( ippi_isset($this->input->post("saksi_value"), '') ? $this->input->post("saksi_value") : false );
        $us             = ( ippi_isset($this->input->post("us"), '') ? $this->input->post("us") : false );
        $pelaku         = ( $us ? 'Pelaku Dikenal' : 'Pelaku Tidak Dikenal' );
        
        $preview        = '<h1>Tinjauan Data</h1>';
        
        if($saksi){
            $hub_kat    = ippi_isset($this->input->post("saksi_hub_kat"), '');
            if( $hub_kat == 1 )     { $kat = 'Keluarga'; }
            elseif( $hub_kat == 2 ) { $kat = 'Orang Dikenal'; }
            else                    { $kat = 'Orang Tak Dikenal'; }
            
            $preview   .= '
            <div class="well" style="padding: 15px 0">
                <div class="col-md-12">
                    <h4 class="preview-title">Data Saksi</h4>
                    <ul class="list-default">
						<li><dl class="dl-horizontal" style="margin: 0;"><dt>Nama Lengkap</dt><dd style="margin-bottom: 0;">'.ippi_isset($this->input->post("saksi_name"), '').'</dd></dl></li>
                        <li><dl class="dl-horizontal" style="margin: 0;"><dt>Tanggal Lahir</dt><dd style="margin-bottom: 0;">'.date( "Y-m-d", strtotime(ippi_isset($this->input->post("saksi_tgllahir"), '')) ).'</dd></dl></li>
                        <li><dl class="dl-horizontal" style="margin: 0;"><dt>No KTP/SIM/PASSPORT</dt><dd style="margin-bottom: 0;">'.ippi_isset($this->input->post("saksi_idcard"), '').'</dd></dl></li>
                        <li><dl class="dl-horizontal" style="margin: 0;"><dt>Propinsi</dt><dd style="margin-bottom: 0;">'.get_name( ippi_isset($this->input->post("saksi_propinsi"), ''), 'state' ).'</dd></dl></li>
                        <li><dl class="dl-horizontal" style="margin: 0;"><dt>Kota/Kabupaten</dt><dd style="margin-bottom: 0;">'.get_name( ippi_isset($this->input->post("saksi_kota"), ''), 'city' ).'</dd></dl></li>
                        <li><dl class="dl-horizontal" style="margin: 0;"><dt>Alamat Lengkap</dt><dd style="margin-bottom: 0;">'.ippi_isset($this->input->post("saksi_alamat"), '').'</dd></dl></li>
                        <li><dl class="dl-horizontal" style="margin: 0;"><dt>Telp/HP</dt><dd style="margin-bottom: 0;">'.ippi_isset($this->input->post("saksi_telp"), '').'</dd></dl></li>
                        <li><dl class="dl-horizontal" style="margin: 0;"><dt>Email</dt><dd style="margin-bottom: 0;">'.ippi_isset($this->input->post("saksi_email"), '').'</dd></dl></li>
                        <li><dl class="dl-horizontal" style="margin: 0;"><dt>Hubungan Kategori</dt><dd style="margin-bottom: 0;">'.$kat.'</dd></dl></li>
                        <li><dl class="dl-horizontal" style="margin: 0;"><dt>Hubungan</dt><dd style="margin-bottom: 0;">'.ucfirst( ippi_isset($this->input->post("saksi_hub"), '') ).'</dd></dl></li>
					</ul>
                </div>
            </div>';
        }
        
        $preview       .= '<div class="well" style="padding: 15px 0">';
        $preview       .= '
        <div class="col-md-4">
            <h4 class="preview-title">Data Korban</h4>
            <dl>
                <dt>Nama Lengkap</dt><dd>'.ippi_isset($this->input->post("korban_name"), '').'</dd>
                <dt>Tanggal Lahir</dt><dd>'.date( "Y-m-d", strtotime(ippi_isset($this->input->post("korban_tgllahir"), '')) ).'</dd>
                <dt>No KTP/SIM/PASSPORT</dt><dd>'.ippi_isset($this->input->post("korban_idcard"), '').'</dd>
                <dt>Propinsi</dt><dd>'.get_name( ippi_isset($this->input->post("korban_propinsi"), ''), 'state' ).'</dd>
                <dt>Kota/Kabupaten</dt><dd>'.get_name( ippi_isset($this->input->post("korban_kota"), ''), 'city' ).'</dd>
                <dt>Alamat Lengkap</dt><dd>'.ippi_isset($this->input->post("korban_alamat"), '').'</dd>
                <dt>Telp/HP</dt><dd>'.ippi_isset($this->input->post("korban_hp"), '').'</dd>
                <dt>Email</dt><dd>'.ippi_isset($this->input->post("korban_email"), '').'</dd>
                <dt>Status HIV</dt><dd>'.ucfirst( ippi_isset($this->input->post("korban_statushiv"), '') ).'</dd>
            </dl>
        </div>';
            
        if( $us ){
            $preview   .= '
            <div class="col-md-4">
                <h4 class="preview-title">Data Pelaku</h4>
                <dl>
                    <dt>Jenis Kelamin</dt><dd>'.ucfirst( ippi_isset($this->input->post("pelaku_jkus"), '') ).'</dd>
                    <dt>Warna Kulit</dt><dd>'.ippi_isset($this->input->post("pelaku_warkul"), '').'</dd>
                    <dt>Tinggi (cm)</dt><dd>'.ippi_isset($this->input->post("pelaku_tinggi"), '').'</dd>
                    <dt>Tanda Lain (Tato, Tanda Lahir, Cacat, Lainnya)</dt><dd>'.ippi_isset($this->input->post("pelaku_tanda"), '').'</dd>
                    <dt>Karakter Tubuh (Kurus, Gemuk, Proporsional, Lainnya)</dt><dd>'.ippi_isset($this->input->post("pelaku_karakter"), '').'</dd>
                    <dt>Ciri Wajah (Kumis, Codet, Alis Tebal, Lainnya)</dt><dd>'.ippi_isset($this->input->post("pelaku_ciwaj"), '').'</dd>
                </dl>
            </div>';
        }else{
            $hub_kat    = ippi_isset($this->input->post("pelaku_hub_kat"), '');
            if( $hub_kat == 1 )     { $kat = 'Keluarga'; }
            elseif( $hub_kat == 2 ) { $kat = 'Orang Dikenal'; }
            else                    { $kat = 'Orang Tak Dikenal'; }
            
            $preview   .= '
            <div class="col-md-4">
                <h4 class="preview-title">Data Pelaku</h4>
                <dl>
                    <dt>Nama Lengkap</dt><dd>'.ippi_isset($this->input->post("pelaku_name"), '').'</dd>
                    <dt>Nama Panggilan</dt><dd>'.ippi_isset($this->input->post("pelaku_nick"), '').'</dd>
                    <dt>Jenis Kelamin</dt><dd>'.ucfirst( ippi_isset($this->input->post("pelaku_jk"), '') ).'</dd>
                    <dt>No KTP/SIM/PASSPORT</dt><dd>'.ippi_isset($this->input->post("pelaku_noid"), '').'</dd>
                    <dt>Hubungan Kategori</dt><dd>'.$kat.'</dd>
                    <dt>Hubungan</dt><dd>'.ucfirst( ippi_isset($this->input->post("pelaku_hub"), '') ).'</dd>
                    <dt>Alamat Lengkap</dt><dd>'.ippi_isset($this->input->post("pelaku_alamat"), '').'</dd>
                    <dt>Telp/HP</dt><dd>'.ippi_isset($this->input->post("pelaku_telp"), '').'</dd>
                    <dt>Email</dt><dd>'.ippi_isset($this->input->post("pelaku_email"), '').'</dd>
                </dl>
            </div>';
        }
        
        $jenis_kasus    = ippi_isset($this->input->post("dk_jenis"), '');
        $kasus          = '';
        foreach($jenis_kasus as $row){
            $kasus     .= ucwords($row) . ', ';
        }
        $kasus          = rtrim($kasus, ", ");
        
        $preview       .= '
        <div class="col-md-4">
            <h4 class="preview-title">Deskripsi Kejadian</h4>
            <dl>
                <dt>Jenis Kekerasan</dt><dd>'.$kasus.'</dd>
                <dt>Jenis Lokasi Kejadian</dt><dd>'.ippi_isset($this->input->post("dk_lokasi"), '').'</dd>
                <dt>Propinsi Kejadian</dt><dd>'.get_name( ippi_isset($this->input->post("dk_propinsi"), ''), 'state' ).'</dd>
                <dt>Kota/Kabupaten Kejadian</dt><dd>'.get_name( ippi_isset($this->input->post("dk_kota"), ''), 'city' ).'</dd>
                <dt>Lokasi Kejadian</dt><dd>'.ippi_isset($this->input->post("dk_alamat"), '').'</dd>
                <dt>Deskripsi Kejadian</dt><dd>'.ucfirst( ippi_isset($this->input->post("dk_deskripsi"), '') ).'</dd>
            </dl>
        </div>';
        $preview       .= '<div style="clear: both;"></div>';
        $preview       .= '</div>';
        
        echo $preview;
    }
}

/* End of file case_reporting.php */
/* Location: ./application/controllers/case_reporting.php */